package com.itheima.Demo01_jdbc;

import java.sql.*;
//通过jdbc 连接数据库查询数据
public class demo01 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        //DriverManager.registerDriver(new com.mysql.jdbc.Driver());//显示注册 会注册两次
        Class.forName("com.mysql.jdbc.Driver");//反射注册驱动
        //获取连接对象 并且 指定连接数据库路径  用户名  密码
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day05_01", "root", "root");
        //获取发送sql语句对象
        Statement statement = connection.createStatement();
        //next遍历结果集
        //释放资源
        ResultSet set = statement.executeQuery(" SELECT * FROM  user ");//发送sql语句
        while (set.next()){//循环遍历

            System.out.println
                    (set.getString("id")
                            +":"+set.getString("username")
                            +":"+set.getString("password"));//根据字段名字打印
        }
       set.close();//释放资源
       statement.close();
       connection.close();

    }
}
//通过jdbc 连接数据库 增加
class jdbc_Insert{
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //反射注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接对象
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day05_01", "root", "root");
        //获取sta对象
        Statement statement = connection.createStatement();
        int i = statement.executeUpdate(" INSERT INTO user values(null,'dale','1010')");
        if (i>0){
            System.out.println("添加成功！");
        }
        //释放资源
        statement.close();
        connection.close();
    }
}
//通过jdbc 连接数据库 修改
class jdbc_Update{
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //反射注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接对象
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day05_01", "root", "root");
        //获取sta对象
        Statement statement = connection.createStatement();
        int i = statement.executeUpdate(" UPDATE user SET password=1105 WHERE id=4 ");
        if (i>0){
            System.out.println("修改成功！");
        }
        statement.close();
        connection.close();
    }
}
//通过jdbc 连接数据库 删除
class jdbc_Delete{
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //反射注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接对象
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day05_01", "root", "root");
        //获取sta 对象
        Statement statement = connection.createStatement();
        //发送sql语句
        int i = statement.executeUpdate(" DELETE FROM user WHERE id=4");
        if (i>0){
            System.out.println("删除成功！");
        }
        //释放资源
        statement.close();
        connection.close();
    }
}
